package com.up.upzl.base.config;

import java.util.List;

import javax.persistence.EntityManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;

import com.google.common.collect.Lists;

/**
 * 自定义RepositoryRestMvcConfiguration
 * 
 * @author geeks
 *
 */
@Configuration
public class CustomRepositoryRestMvcConfiguration extends RepositoryRestConfigurerAdapter {

	@Autowired
	private EntityManager em;

	/**
	 * 配置返回ID
	 */
	@Override
	public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
		List<Class<?>> entities = Lists.newArrayList();
		em.getEntityManagerFactory().getMetamodel().getEntities().forEach(entity -> {
			entities.add(entity.getJavaType());
		});
		config.exposeIdsFor(entities.toArray(new Class[] {}));
	}

}
